<?php
namespace app\admin\controller;

class User extends Common
{
    public function user_list()
    {
        $ret = db('user')->order("user_addtime desc")->paginate(3);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',1);
        return view('user_list');
    }

    public function search_user()
    {
        $search_str = input('get.search_str');
        $data = db("user")
        ->where("user_name like '{$search_str}%' or user_email like '{$search_str}%' ")->select();
        //dump($data);
        if( empty($data) )
        {
            return 
            ['code'=>0,'msg'=>"查询失败"];
        }
        else
        {
            $this->assign('data',$data);
            $html = $this->fetch('user_liebiao');
            $this->success("成功","",$html);
            return view('user_list');
        }
    }

    pubLic function user_pass()
    {
        $user_id = input('get.user_id');
        $ret = db('user')->where("user_id={$user_id}")->find();
        if($ret['user_status']== '1'){
            return [
                'msg'    => '已是启用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('user')->where("user_id={$user_id}")->update(['user_status'=>'1']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }
    pubLic function user_fail()
    {
        $user_id = input('get.user_id');
        $ret = db('user')->where("user_id={$user_id}")->find();
        if($ret['user_status']== '0')
        {
            return [
                'msg'    => '已是禁用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('user')->where("user_id={$user_id}")->update(['user_status'=>'0']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }

    public function user_del()
    {
        $user_id = input('get.user_id');
        $ret = db('user')->where("user_id={$user_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

}
